package com.welbell.recipes;


import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.databinding.BindingAdapter;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableArrayMap;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;
import androidx.databinding.ObservableMap;

import com.squareup.picasso.Picasso;
import com.welbell.recipes.bean.UserBean;
import com.welbell.recipes.databinding.ActivityMainBinding;
import com.welbell.utlis.TLog;


public class MainActivity extends BaseActivity<ActivityMainBinding> {

    private static final String TAG = MainActivity.class.getName();

    ObservableField<String> inputs = new ObservableField<>("演示ObservableField");
    ObservableMap<String, String> maps = new ObservableArrayMap<>();
    ObservableList<String> lists = new ObservableArrayList<>();
    int sign = 0, listSign = 0, age = 0;
    boolean isName = true;

    @Override
    protected int getLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        dataBinding.setClickHandler(clickHandler);
        dataBinding.setInput(inputs);

        dataBinding.setContents("演示DataBinding数据绑定");
        handler.removeMessages(0);
        handler.sendEmptyMessageDelayed(0, 2000);
    }

    ClickHandler clickHandler = new ClickHandler() {
        @Override
        public void onSubmit(View v) {
            Log.d(TAG, "onSubmit: 1");
        }

        @Override
        public void onConfirm(View v) {
            Log.d(TAG, "onConfirm: 1");
        }
    };


    Handler handler = new Handler(Looper.myLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    sign++;
                    dataBinding.setContents("演示DataBinding数据绑定: " + sign);
                    handler.removeMessages(0);
                    handler.sendEmptyMessageDelayed(0, 2000);
                    TLog.d(TAG, dataBinding.getContents());
                    break;
                case 1:
                    if (isName) {
                        isName = false;
                        dataBinding.setMapKeys("name");
                    } else {
                        isName = true;
                        dataBinding.setMapKeys("age");
                    }
                    handler.removeMessages(1);
                    handler.sendEmptyMessageDelayed(1, 2000);
                    break;
                case 2:
                    if (listSign < 5) {
                        dataBinding.setListSubscript(listSign);
                        listSign++;
                    } else {
                        listSign = 0;
                        dataBinding.setListSubscript(listSign);
                    }
                    handler.removeMessages(2);
                    handler.sendEmptyMessageDelayed(2, 2000);
                    break;
                case 3:
                    age++;
                    UserBean userBean = new UserBean("tao", age);
                    dataBinding.setUser(userBean);

                    handler.removeMessages(3);
                    handler.sendEmptyMessageDelayed(3, 2000);
                    break;
            }

        }
    };

    @Override
    protected void initWork() {
    }

    @Override
    protected void data() {
        maps.clear();
        maps.put("name", "Map集合演示：1");
        maps.put("age", "Map集合演示：2");
        dataBinding.setMap(maps);
        lists.clear();
        for (int i = 0; i < 5; i++) {
            lists.add("list集合数据演示：" + i);
        }
        dataBinding.setList(lists);
        handler.removeMessages(1);
        handler.sendEmptyMessageDelayed(1, 2000);
        handler.removeMessages(2);
        handler.sendEmptyMessageDelayed(2, 2000);

        handler.removeMessages(3);
        handler.sendEmptyMessageDelayed(3, 2000);
        dataBinding.setNetworkPictures("https://x0.ifengimg.com/ucms/2022_10/8A28619D49C8DE5999F84E9101E304F4F9DA73E1_size127_w1008_h435.jpg");
        dataBinding.setLocalPictures(R.mipmap.ic_launcher);
    }


    @BindingAdapter("image")
    public static void setImage(ImageView v, String url) {
        if (!TextUtils.isEmpty(url)) {
            Picasso.get().load(url).into(v);
        } else {
            v.setBackgroundColor(Color.GRAY);
        }
    }

    @BindingAdapter(value = {"netWorkImage","defaultLocalImage"},requireAll = false)
    public static void setImage(ImageView v, String url,int rid){
        if (!TextUtils.isEmpty(url)) {
            Picasso.get().load(url).placeholder(R.mipmap.ic_launcher).into(v);
        } else {
            v.setImageResource(rid);
        }
    }

}